<?php
declare (strict_types = 1);

namespace sheep\middleware;

use sheep\facade\Auth;
use sheep\exception\SheepException;
use think\facade\Cookie;
use think\facade\Config;

class ResponseHeader
{
    public function handle($request, \Closure $next, $guard = null)
    {
        $response = $next($request);

        // 登录token
        $token = session('header_authorization');
        if ($token) {
            // 清除 session
            session('header_authorization', null);
            if (in_array('cookie', Config::get('jwt.token_mode'))) {
                Cookie::set('token', $token);
            }

            if (in_array('header', Config::get('jwt.token_mode'))) {
                $response = $response->header([
                    'Authorization' => 'Bearer ' . $token,
                    'Access-Control-Expose-Headers' => 'Authorization'
                ]);
            }
        }

        return $response;
    }
}
